package com.qf.mrmd.service.impl;

import com.qf.mrmd.dao.PicDao;
import com.qf.mrmd.entity.Pic;
import com.qf.mrmd.result.ResponseCode;
import com.qf.mrmd.result.ResponseData;
import com.qf.mrmd.service.PicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (Pic)表服务实现类
 *
 * @author makejava
 * @since 2021-07-12 16:16:52
 */
@Service("picService")

public class PicServiceImpl implements PicService {

    @Resource
    private PicDao picDao;

    @Override
    public Pic queryById(Integer pId) {
        Pic pic = new Pic();
        pic.setpId(pId);
        pic.setType("lb");
        pic.setStatus(1);
        return pic;
    }

    @Override
    public List<Pic> queryAllByLimit(int offset, int limit) {
        return null;
    }

//    新增数据
    @Transactional
    @Override
    public Pic insert(Pic pic) {
        int i = picDao.insert(pic);
        int i1=1/0;
        return pic;
    }

//    修改数据
    @Override
    public Pic update(Pic pic) {
        return null;
    }

//    通过主键删除数据
    @Override
    public boolean deleteById(Integer pId) {
        return false;
    }

    @Transactional
    @Override
    public ResponseData savePicInfo(Pic pic) {
        try {
            pic.setStatus(0);
            int i = picDao.insert(pic);
            int i1 = 2/0;
            if (i>0){
                return new ResponseData(ResponseCode.SUCCESS,pic);
            }
            return new ResponseData(ResponseCode.FAILED,null);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ResponseCode.FAILED,null);
        }

    }


    @Override
    public ResponseData queryByType(String type,Integer status) {
        Pic pic = new Pic();
        pic.setType(type);
        pic.setStatus(status);
        List<Pic> pics = picDao.queryAll(pic);
        Integer index = 0;
        for (int i = 0; i < pics.size(); i++) {
            Pic pic1 = pics.get(i);
            String title = pic1.getTitle();
            if (title.equals("推荐")){
                index=i;
                break;
            }
        }
        Collections.swap(pics,index,0);
        return new ResponseData(ResponseCode.SUCCESS,pics);
    }


    /**
     * 作业：查询所有图片
     * @return
     */
    @Override
    public ResponseData queryAllWork() {
        Pic pic = new Pic();
        List<Pic> pics = picDao.queryAllWork(pic);
        return new ResponseData(ResponseCode.SUCCESS,pics);
    }

    /**
     * \作业：插入图片
     * @param pic
     * @return
     */
    @Override
    public ResponseData insertWork(Pic pic) {
        int i = picDao.insertWork(pic);
        if (i>0){
            return new ResponseData(ResponseCode.SUCCESS,pic);
        }
        return new ResponseData(ResponseCode.FAILED,null);
    }

    @Override
    public ResponseData delWork(Integer id){
        int i = picDao.delWork(id);
        if (i>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.FAILED);
    }

    @Override
    public ResponseData updateWork(Integer id) {
        Pic pic = picDao.queryById(id);
//        String s = "hello";
//        pic.setTitle(s);
        int i = picDao.update(pic);
        if (i>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.FAILED);
    }

}
